#include "DeviceGroupTableModel.h"


DeviceGroupTableModel::DeviceGroupTableModel(QObject *)
{

}

int DeviceGroupTableModel::rowCount(const QModelIndex &) const
{
    return deviceList.size();
}

int DeviceGroupTableModel::columnCount(const QModelIndex &) const
{
     return 6;
}

QVariant DeviceGroupTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role == Qt::DisplayRole) {
        const auto &device = deviceList[index.row()];
        switch (index.column()) {
        case 0: return device.name;
        case 1: return device.type;
        case 2: return device.location;
        case 3: return device.manufacturer;
        case 4: return device.model;
        case 5: return device.status;
        case 6: return device.installation_date;
        default: return QVariant();
        }
    }else if (role == Qt::TextAlignmentRole) {
        return Qt::AlignCenter;
    }
    return QVariant();
}

QVariant DeviceGroupTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
        switch (section) {
        case 0: return "设备名称";
        case 1: return "设备类型";
        case 2: return "设备位置";
        case 3: return "设备制造商";
        case 4: return "设备型号";
        case 5: return "设备状态";
        case 6: return "安装日期";
        default: return QVariant();
        }
    }
    return QVariant();
}

void DeviceGroupTableModel::updateAllDeviceList(const QList<deviceModel> &newList)
{
    beginResetModel();
    deviceList = newList;
    endResetModel();
}
